import { createRouter, createWebHistory } from 'vue-router'
import { getToken } from '@/utils/cookie'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: "/oj/login",
      name: "login",
      component: () => import("@/views/Login.vue"), //@ 和 .. 现在都一样 @ 代表一个路径得别名
    },
    {
      path: "/", // 让初始页面重定向，直接跳转到登录页
      redirect:'/oj/login',
    },
    {
      path: '/oj/layout',
      name: 'layout',
      component: () => import('@/views/Layout.vue'),
      children: [
          {  
            path: "question",
            name: "question",
            component: () => import("@/views/Question.vue"),
          },
          {  
            path: "exam",
            name: "exam",
            component: () => import("@/views/Exam.vue"),
          },
          {  
            path: "cuser",
            name: "cuser",
            component: () => import("@/views/Cuser.vue"),
          }
        ]
      },
    ]
  }
)


router.beforeEach((to, from, next) => {
  if (getToken()) {  //已经登陆过
    /* has token*/
    if (to.path === '/oj/login') {
      next({ path: '/oj/layout' })
    } else {
      next()
    }
  } else {
    if (to.path !== '/oj/login') {
      next({path:'/oj/login'})
    } else {
      next()
    }
  }
})



export default router
